require "Buffer"
require "Person"
require "Phone"

local f = io.open("test.bin", "rb")
local t = f:read("*all")
local msg = {}
SetIndex(msg, Phone)

msg:Parse(CreateBuffer(t))

print("####1Dump Phone")
DumpPB(msg)



local f = io.open("test1.bin", "rb")
local t = f:read("*all")
f:close()


local msg = {}
SetIndex(msg, Person)
msg:Parse(CreateBuffer(t))

print("####2Dump Person")
DumpPB(msg)




local ph = {}
SetIndex(ph, Phone)
ph.num = "131233449"

print("####3Serialize Phone")
local buf = ph:Serialize()
local ret = buf:Finish()
print(ret)

local f1 = io.open("out.bin", "wb")
f1:write(ret)
f1:close()


local ph1 = {}
SetIndex(ph1, Phone)
ph1:Parse(CreateBuffer(ret))

print("####4Dump Encoded Phone")
DumpPB(ph1)




local ph = {}
SetIndex(ph, Phone)
ph.num = "123"


local per = {}
SetIndex(per, Person)
per.id = 22
per.name = "guagua"
per.email = "cao@ji.net"
per.phone = ph

local buf = per:Serialize()
local ret = buf:Finish()
local f1 = io.open("out2.bin", "wb")
f1:write(ret)
f1:close()

local per1 = {}
SetIndex(per1, Person)
per1:Parse(CreateBuffer(ret))
print("####5Dump Encoded Person")
DumpPB(per1)


